//
//  YGPatientHomeViewController.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/3/16.
//

#import "YGPatientHomeViewController.h"
#import "YGHomeBLL.h"
#import <Masonry/Masonry.h>
#import "YGLocalTableViewCell.h"
#import "YGEnterTableViewCell.h"
#import "YGExpertsTableViewCell.h"
#import "YGPatientHomeModel.h"
#import "SVHomeTopicBannerView.h"
#import "YGExpertsModel.h"
#import "YGHospitalDetailsViewController.h"
#import "YGFoodmanagerView.h"
#import "YGFoodManagerViewController.h"
#import "YGConsultView.h"
#import "YGConsultViewController.h"
#import "YGOwnhelpView.h"
#import "YGOwnHelpViewController.h"
#import "YGScienceCenterView.h"
#import "YGScienceCenterViewController.h"

@interface YGPatientHomeViewController () < UITableViewDelegate,UITableViewDataSource,FXHomeBannerViewDelegate,FXFoodMannagerViewDelegate
,YGEnterTableViewCellDelegate,FXConsultViewDelegate,FXOwnHelpViewDelegate,FXScienceViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) YGPatientHomeModel  *model;
@property (nonatomic, strong) SVHomeTopicBannerView  *bannerView;

@end

@implementation YGPatientHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestData];
    [self initView];
    [self initContraints];
    // Do any additional setup after loading the view.
}

- (void)initView{
    [self.view addSubview:self.tableView];
}

- (void)initContraints{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(self.view);
    }];
}
- (void)requestData{
//    [self showWithStatus:@"请求网络ing..."];
    WeakSelf(self);
    [YGHomeBLL requestHomeInfoSuccess:^(YGPatientHomeModel * _Nonnull model) {
        weakself.model = model;
        [weakself reloadData];
    } fail:^(NSError * _Nonnull error) {
        
    }];
}
//首页数据
- (void)reloadData{
    //banner图片数据接口
    NSMutableArray *array = [NSMutableArray array];
    for (PosterModel *poster in self.model.poster) {
        YGBannerModel *bannerModel = [[YGBannerModel alloc] init];
        bannerModel.imgUrl = poster.imgUrl;
  //      bannerModel.name = poster.name;
        [array addObject:bannerModel];
    }
    [self.bannerView updateContentInfo:array];
    
    
    
    [self.tableView reloadData];
}
#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        return 109;
    }else if (indexPath.section == 1){
        return 370;
    }else{
        return 150;
    }
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 10;
}
 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.00001;
    }else
        return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        YGHospitalDetailsViewController *vc = [[YGHospitalDetailsViewController alloc]init];
        //修改返回按钮的文字
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
        barButtonItem.tintColor=UIColorFromRGB(0xffffff);
        self.navigationItem.backBarButtonItem = barButtonItem;
        
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0){
        YGLocalTableViewCell *cell = [[YGLocalTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YGLocalTableViewCell"];
        [cell configureData:self.model.hospitals];
        return cell;
    }else if (indexPath.section == 1){
        YGEnterTableViewCell *cell = [[YGEnterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YGEnterTableViewCell"];
        cell.delegate = self;
        
        return cell;
    }else{
        YGExpertsTableViewCell *cell = [[YGExpertsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YGExpertsTableViewCell"];
        [cell configureData:self.model.expert];

        return cell;
    }
    
}

#pragma delegate - 二级页面跳转

-(void)jumpConsultPage{
        YGConsultViewController *vc = [[YGConsultViewController alloc]init];
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
        barButtonItem.tintColor = UIColorFromRGB(0x333333);
    
        [self.navigationController pushViewController:vc animated:YES];
}

-(void)jumpOwnHelpPage{
    YGOwnHelpViewController *vc = [[YGOwnHelpViewController alloc]init];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    barButtonItem.tintColor = UIColorFromRGB(0x333333);

    [self.navigationController pushViewController:vc animated:YES];
}


- (void)jumpFoodManagerPage{
    
        YGFoodManagerViewController *vc = [[YGFoodManagerViewController alloc]init];
    
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
        barButtonItem.tintColor=[UIColor whiteColor];
        self.navigationItem.backBarButtonItem = barButtonItem;
    
        [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)jumpScienceCenterPage{
    YGScienceCenterViewController *vc = [[YGScienceCenterViewController alloc]init];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    barButtonItem.tintColor = UIColorFromRGB(0x333333);

    [self.navigationController pushViewController:vc animated:YES];
}



#pragma mark - bannerDelegate
- (void)onBannerClick:(NSInteger)index{
    
}


#pragma mark - setter

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:YGEnterTableViewCell.class forCellReuseIdentifier:@"YGEnterTableViewCell"];
        [_tableView registerClass:YGLocalTableViewCell.class forCellReuseIdentifier:@"YGLocalTableViewCell"];
        [_tableView registerClass:YGExpertsTableViewCell.class forCellReuseIdentifier:@"YGExpertsTableViewCell"];
        _tableView.tableHeaderView = self.bannerView;
        
        
    }
    return _tableView;
}

- (SVHomeTopicBannerView *)bannerView{
    if (!_bannerView) {
        CGFloat height = 134/375.0 * self.view.width;
        _bannerView = [[SVHomeTopicBannerView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, height)];
        _bannerView.homeBannerViewDelegate = self;
    }
    return _bannerView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
